//
//  LSTextMsgCell.m
//  LiveStar
//
//  Created by 漪珊 on 2017/4/21.
//  Copyright © 2017年 apple. All rights reserved.
//

#import "LSTextMsgCell.h"

@implementation LSTextMsgCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.backgroundColor = [UIColor groupTableViewBackgroundColor ];
        [self addSubview:self.msgTextLabel];
    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    float y = self.avatarImgView.origin.y + 11;
    float x = self.avatarImgView.origin.x + (self.message.msgOwnerType == LSMsgOwnerTypeSelf ? - self.msgTextLabel.width - 27 : self.avatarImgView.width + 23);
    [self.msgTextLabel setOrigin:CGPointMake(x, y)];
    
    x -= 18;                                    // 左边距离头像 5
    y = self.avatarImgView.origin.y;       // 上边与头像对齐 (北京图像有5个像素偏差)
    float h = MAX(self.msgTextLabel.height + 20, self.avatarImgView.height);
    [self.msgBackImgView setFrame:CGRectMake(x, y, self.msgTextLabel.width + 40, h)];
}

- (void)setMessage:(LSMessage *)message {
    [super setMessage:message];
    [_msgTextLabel setAttributedText:message.attrText];
    [_msgTextLabel setText:message.text];
    _msgTextLabel.size = message.messageSize;
}

- (UILabel *)msgTextLabel
{
    if (_msgTextLabel == nil) {
        _msgTextLabel = [[UILabel alloc] init];
        [_msgTextLabel setFont:[UIFont systemFontOfSize:16.0f]];
        [_msgTextLabel setNumberOfLines:0];
    }
    return _msgTextLabel;
}

@end
